from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

class PaymentPage:
    def __init__(self, driver):
        self.driver = driver
        # 页面元素定位器
        self.card_number_input_locator = (By.ID, "card-number")
        self.expiry_date_input_locator = (By.ID, "expiry-date")
        self.ccv_input_locator = (By.ID, "ccv")
        self.name_on_card_input_locator = (By.ID, "name-on-card")
        self.submit_payment_button_locator = (By.ID, "submit-payment")
        self.payment_status_locator = (By.ID, "payment-status")
        self.order_confirmation_locator = (By.ID, "order-confirmation")
        self.payment_methods_locator = (By.ID, "payment-methods")
        self.save_card_checkbox_locator = (By.ID, "save-card")
        self.cancel_payment_button_locator = (By.ID, "cancel-payment")

    def navigate(self):
        self.driver.get('http://example.com/payment')

    def enter_card_number(self, card_number):
        card_number_input = WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located(self.card_number_input_locator)
        )
        card_number_input.send_keys(card_number)

    def enter_expiry_date(self, expiry_date):
        expiry_date_input = WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located(self.expiry_date_input_locator)
        )
        expiry_date_input.send_keys(expiry_date)

    def enter_ccv(self, ccv):
        ccv_input = WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located(self.ccv_input_locator)
        )
        ccv_input.send_keys(ccv)

    def enter_name_on_card(self, name):
        name_on_card_input = WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located(self.name_on_card_input_locator)
        )
        name_on_card_input.send_keys(name)

    def submit_payment(self):
        submit_payment_button = WebDriverWait(self.driver, 10).until(
            EC.element_to_be_clickable(self.submit_payment_button_locator)
        )
        submit_payment_button.click()

    def get_payment_status(self):
        payment_status = WebDriverWait(self.driver, 10).until(
            EC.visibility_of_element_located(self.payment_status_locator)
        )
        return payment_status.text

    def get_order_confirmation(self):
        order_confirmation = WebDriverWait(self.driver, 10).until(
            EC.visibility_of_element_located(self.order_confirmation_locator)
        )
        return order_confirmation.text

    def select_payment_method(self, method):
        payment_methods = WebDriverWait(self.driver, 10).until(
            EC.presence_of_all_elements_located(self.payment_methods_locator)
        )
        for method_option in payment_methods:
            if method in method_option.text:
                method_option.click()
                break

    def check_save_card(self):
        save_card_checkbox = WebDriverWait(self.driver, 10).until(
            EC.element_to_be_clickable(self.save_card_checkbox_locator)
        )
        save_card_checkbox.click()

    def cancel_payment(self):
        cancel_payment_button = WebDriverWait(self.driver, 10).until(
            EC.element_to_be_clickable(self.cancel_payment_button_locator)
        )
        cancel_payment_button.click()